const ajax = require('../../../utils/ajax/ajax.js')
Page({
    data: {
        title: '推荐积分',
        point: '',
        recPoint: '',
        earPoint: '',
        recPointList: [],
        earPointList: [],
        freezePoints: '',
        recLoading: true,
        earLoading: true,
        pages: 1
    },
    onLoad: function (options) {
        const { rec_points, sob_points,freeze_points } = options
        
        this.showPoint(rec_points, sob_points, freeze_points)
        this.getUserPointsLog()
        this.getUserPointSob()
    },
    showPoint(rec_points, sob_points, freeze_points) {
        this.setData({
            point: rec_points,
            recPoint: rec_points,
            earPoint: sob_points,
            freezePoints: freeze_points
        })
    },
    // 请求推荐积分
    getUserPointsLog() {
        const that = this
        ajax.post({
            url: '/wx/getUserPointsLog',
            data: {
                page: 1,
                limit: 10,
                type: 1
            },
            success(res) {
                that.setData({
                    recPointList: res.data.list
                })
            }
        })
    },
    // 请求收益积分
    getUserPointSob() {
        const that = this
        ajax.post({
            url: '/wx/getUserPointsLog',
            data: {
                page: 1,
                limit: 10,
                type: 2
            },
            success(res) {
                console.log(res);
                that.setData({
                    earPointList: res.data.list
                })
            }
        })
    },
    onTabsClick(e) {
        const title = e.detail.title
        if (title === '收益积分'){
            this.setData({
                point: this.data.earPoint,
                title: '收益积分'
            })
        } else {
            this.setData({
                point: this.data.recPoint,
                title: '推荐积分'
            })
        }
    },

    onReachBottom () {
        const title = this.data.title
        if (title === '收益积分') {
          (this.data.pages) ++;
          this.reachBottomEarPoints()
          return
        }
    
        if(title === '推荐积分') {
          (this.data.pages) ++;
          this.reachBottomRecPoints()
          return
        }
    },
    
    reachBottomEarPoints() {
        const that = this
        ajax.post({
            url: '/wx/getUserPointsLog',
            data: {
                page: that.data.pages,
                limit: 10,
                type: 2
            },
            success(res) {
                let earPointList = res.data.list
                if (earPointList.length === 0) {
                    that.setData({
                        earLoading: true
                    })
                    return
                }
                
                if (that.data.pages === 1) {
                    that.setData({
                        earPointList: earPointList
                    })
                    return
                }

                if (that.data.pages !== 1) {
                    let newList = that.data.earPointList.concat(earPointList)
                    that.setData({
                        earPointList: newList
                    })
                    return
                }
            }
        })
    },

    reachBottomRecPoints() {
        const that = this
        ajax.post({
            url: '/wx/getUserPointsLog',
            data: {
                page: that.data.pages,
                limit: 10,
                type: 1
            },
            success(res) {
                let recPointList = res.data.list
                if (recPointList.length === 0) {
                    that.setData({
                        recLoading: true
                    })
                    return
                }
                
                if (that.data.pages === 1) {
                    that.setData({
                        recPointList: recPointList
                    })
                    return
                }

                if (that.data.pages !== 1) {
                    let newList = that.data.recPointList.concat(recPointList)
                    that.setData({
                        recPointList: newList
                    })
                    return
                }
            }
        })
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.getUserPointsLog()
        this.getUserPointSob()
        wx.stopPullDownRefresh()
    }
})